//
//  MyGradeJYGCView.m
//  MyGrade
//
//  Created by 田涛 on 2017/9/28.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "MyGradeJYGCView.h"
#import "GlobalHeader.h"

@interface MyGradeJYGCView ()
/** 发布获得*/
@property (nonatomic, strong)  UIButton *fbhdLbl;
/** 获得最佳*/
@property (nonatomic, strong)  UIButton *hdzjLbl;
/** 选取最佳*/
@property (nonatomic, strong)  UIButton *xqzjLbl;
/** 其他经验*/
@property (nonatomic, strong)  UIButton *qtjyLbl;
/** 左横线*/
@property (nonatomic, strong)  UILabel *leftLine;
/** 右横线*/
@property (nonatomic, strong)  UILabel *rightLine;
/** 发布获得 && 选取最佳 && 左横线 ，背景View*/
@property (nonatomic, strong)  UIView *leftBackgroundView;
/** 获得最佳 && 其他经验 && 右横线 ，背景View*/
@property (nonatomic, strong)  UIView *rightBackgroundView;
/** 进度环形*/
@property (nonatomic, strong) SCPieChart *progressView;
@end

@implementation MyGradeJYGCView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
        [self layout];
    }
    return self;
}


/**
 *  初始化
 */
- (void)setup {
    [self addSubview:self.leftBackgroundView];
    [self addSubview:self.rightBackgroundView];
    [self.leftBackgroundView addSubview:self.fbhdLbl];
    [self.leftBackgroundView addSubview:self.leftLine];
    [self.leftBackgroundView addSubview:self.xqzjLbl];
    [self.rightBackgroundView addSubview:self.hdzjLbl];
    [self.rightBackgroundView addSubview:self.rightLine];
    [self.rightBackgroundView addSubview:self.qtjyLbl];
    [self addSubview:self.progressView];
}

- (void)setModel:(MyGradeJYGCModel *)model {
    _model = model;
    
    [self.fbhdLbl setTitle:[NSString stringWithFormat:@"发布获得：%ld",model.fbhdExperience] forState:UIControlStateNormal];
    [self.hdzjLbl setTitle:[NSString stringWithFormat:@"获得最佳：%ld",model.hdzjExperience] forState:UIControlStateNormal];
    [self.xqzjLbl setTitle:[NSString stringWithFormat:@"选取最佳：%ld",model.xqzjExperience] forState:UIControlStateNormal];
    [self.qtjyLbl setTitle:[NSString stringWithFormat:@"其他经验：%ld",model.qtjyExperience] forState:UIControlStateNormal];

    
    // 给环形进度 赋值
    if (self.progressView) {
        [self.progressView removeFromSuperview];
        self.progressView = nil;
    }
    
    NSArray *items = @[
                       [SCPieChartDataItem dataItemWithValue:model.hdzjExperience color:RGB(150, 0, 253) description:nil],
                       [SCPieChartDataItem dataItemWithValue:model.xqzjExperience color:RGB(193, 76, 61) description:nil],
                       [SCPieChartDataItem dataItemWithValue:model.qtjyExperience color:RGB(47, 132, 247) description:nil],
                       [SCPieChartDataItem dataItemWithValue:model.fbhdExperience color:RGB(23, 71, 255) description:nil],
                       ];
    CGFloat width = self.frame.size.height *0.76;
    self.progressView = [[SCPieChart alloc] initWithFrame:CGRectMake((SCREEN_WIDTH -width)/2, (self.frame.size.height - width)/2, width, width) items:items];
    [self.progressView strokeChart];
    [self addSubview:self.progressView];
    
    CGFloat whiteWidth = self.frame.size.height *0.76 -20;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - whiteWidth)/2, (self.frame.size.height - whiteWidth)/2,whiteWidth, whiteWidth)];
    view.layer.cornerRadius  = whiteWidth /2;
    view.layer.masksToBounds = YES;
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
}


/**
 *  适配
 */
- (void)layout {
    CGFloat height = self.frame.size.height;
    
    self.leftBackgroundView.sd_layout
    .leftSpaceToView(self, WidthScale_IOS7(20))
    .centerYEqualToView(self)
    .widthIs(SCREEN_WIDTH *0.29)
    .heightIs(height *0.48);
    
    self.rightBackgroundView.sd_layout
    .rightSpaceToView(self, WidthScale_IOS7(20))
    .centerYEqualToView(self)
    .widthIs(SCREEN_WIDTH *0.29)
    .heightIs(height *0.48);
    
    /** 左边布局*/
    self.fbhdLbl.sd_layout
    .leftSpaceToView(self.leftBackgroundView, 0)
    .topSpaceToView(self.leftBackgroundView, HeightScale_IOS7(5))
    .widthRatioToView(self.leftBackgroundView, 1)
    .heightIs(HeightScale_IOS7(20));
    
    self.leftLine.sd_layout
    .leftEqualToView(self.fbhdLbl)
    .centerYEqualToView(self.leftBackgroundView)
    .widthRatioToView(self.leftBackgroundView, 1)
    .heightIs(1);
    
    self.xqzjLbl.sd_layout
    .leftEqualToView(self.fbhdLbl)
    .bottomSpaceToView(self.leftBackgroundView, HeightScale_IOS7(5))
    .widthRatioToView(self.fbhdLbl, 1)
    .heightIs(HeightScale_IOS7(20));
    
    /** 右边布局*/
    self.hdzjLbl.sd_layout
    .leftSpaceToView(self.rightBackgroundView, 0)
    .topSpaceToView(self.rightBackgroundView, HeightScale_IOS7(5))
    .widthRatioToView(self.rightBackgroundView, 1)
    .heightIs(HeightScale_IOS7(20));
    
    self.rightLine.sd_layout
    .leftSpaceToView(self.rightBackgroundView, 0)
    .rightSpaceToView(self.rightBackgroundView, 0)
    .centerYEqualToView(self.rightBackgroundView)
    .heightIs(1);
    
    self.qtjyLbl.sd_layout
    .leftSpaceToView(self.rightBackgroundView, 0)
    .bottomSpaceToView(self.rightBackgroundView, HeightScale_IOS7(5))
    .widthRatioToView(self.rightBackgroundView, 1)
    .heightIs(HeightScale_IOS7(20));
}


#pragma mark - 懒加载

- (UIView *)leftBackgroundView {
    if (!_leftBackgroundView) {
        _leftBackgroundView = [[UIView alloc] init];
    }
    return _leftBackgroundView;
}

- (UIView *)rightBackgroundView {
    if (!_rightBackgroundView) {
        _rightBackgroundView = [[UIView alloc] init];
    }
    return _rightBackgroundView;
}

- (UIButton *)fbhdLbl {
    if (!_fbhdLbl) {
        _fbhdLbl = [[UIButton alloc] init];
        _fbhdLbl.titleLabel.font = [UIFont systemFontOfSize:WidthScale_IOS7(12)];
        [_fbhdLbl setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _fbhdLbl;
}

- (UILabel *)leftLine {
    if (!_leftLine) {
        _leftLine = [[UILabel alloc] init];
        _leftLine.backgroundColor = RGB(239, 239, 239);
    }
    return _leftLine;
}

- (UIButton *)xqzjLbl {
    if (!_xqzjLbl) {
        _xqzjLbl = [[UIButton alloc] init];
        _xqzjLbl.titleLabel.font = [UIFont systemFontOfSize:WidthScale_IOS7(12)];
        [_xqzjLbl setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _xqzjLbl;
}

- (UIButton *)hdzjLbl {
    if (!_hdzjLbl) {
        _hdzjLbl = [[UIButton alloc] init];
        _hdzjLbl.titleLabel.font = [UIFont systemFontOfSize:WidthScale_IOS7(12)];
        [_hdzjLbl setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _hdzjLbl;
}

- (UILabel *)rightLine {
    if (!_rightLine) {
        _rightLine = [[UILabel alloc] init];
        _rightLine.backgroundColor = RGB(239, 239, 239);
    }
    return _rightLine;
}

- (UIButton *)qtjyLbl {
    if (!_qtjyLbl) {
        _qtjyLbl = [[UIButton alloc] init];
        _qtjyLbl.titleLabel.font = [UIFont systemFontOfSize:WidthScale_IOS7(12)];
        [_qtjyLbl setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _qtjyLbl;
}
@end

